<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row>
        <el-col :span="12">
          <el-form-item label="配件分类" required prop="assetTypeCode">
            <TreeSelect
              v-model="formItem.assetTypeCode"
              :multiple="false"
              :options="typeOptions"
              placeholder="选择配件分类"
              :disable-branch-nodes="true"
              @select="selcetType"
              @input="inputChange"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="配件名称" prop="productId">
            <el-select v-model="formItem.productId" placeholder="请选择配件" style="width:100%" @change="productChange">
              <el-option v-for="(item) in productList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-input v-model="formItem.standard" placeholder="请选择规格型号" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="规格型号" prop="standard">
            <el-input v-model="formItem.standard" placeholder="请选择规格型号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="count">
            <el-input-number v-model="formItem.count" :min="formItem.acceptedCount ? formItem.acceptedCount : 1" :max="999999" :precision="0" label="数量" style="width:100%" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="单价" prop="amount">
            <el-input-number v-model="formItem.amount" :min="1" :max="999999" :precision="2" label="单价" style="width:100%" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="描述" prop="description">
            <el-input v-model="formItem.description" placeholder="请输入详细描述" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>

</template>

<script>
import { tree } from '@neu/dcam-ui/api/spareType'
// import { list } from '@neu/dcam-ui/api/goodsFile'
import TreeSelect from '@neu/biz-core/components/TreeSelect'
// 定义一个表单原始项
const formItem = {
  assetTypeCode: null,
  assetTypeName: '',
  productName: '',
  productId: '',
  count: 1,
  amount: '',
  description: '',
  standard: '',
  type: '2'
}
export default {
  name: 'ContractGoodsfileEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 数据
    obj: {
      required: false,
      type: Object,
      default: null
    },
    belongs: {
      type: String,
      default: null,
      required: true
    },
    categories: {
      type: String,
      default: null,
      required: false
    }
  },

  data() {
    return {
      // 表单
      formItem: { ...formItem },
      /** 资产类型树 */
      typeOptions: [],
      // 标准化产品
      productList: [],
      // 标记
      init: true,
      // 表单验证规则
      rules: {
        assetTypeCode: [{ required: true, message: '请选择分类', trigger: 'blur' }],
        productId: [{ required: true, message: '请选择指定的产品', trigger: 'blur' }],
        count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        amount: [{ required: true, message: '最多两位小数', trigger: 'blur' }]
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    obj: {
      handler() {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    // 分类树
    tree().then(response => {
      const result = response.data
      this.typeOptions = result
    })
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      this.formItem = { ...formItem }
      if (this.obj) {
        this.init = false
        this.formItem = { ...this.obj }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            resolve(this.formItem)
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      // 重置表单
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    /** 类型发生变化 */
    inputChange(value) {
      if (this.init) {
        this.productList = []
        // this.formItem.assetTypeName = ''
        this.formItem.productId = ''
        this.formItem.standard = ''
        this.formItem.productName = ''
      }
      this.init = true
      // if (value) {
      //   list({ s_spareTypeCode: this.formItem.assetTypeCode }).then(res => {
      //     this.productList = res.data
      //   })
      // }
    },
    /** 分类赋值 */
    selcetType(value) {
      this.formItem.assetTypeName = value.title
    },
    /** 产品发生变化 */
    productChange(value) {
      this.formItem.standard = ''
      this.formItem.productName = ''
      this.formItem.amount = ''
      if (value) {
        const filterData = this.productList.filter(e => e.id === value)
        this.formItem.productName = filterData[0].name
        this.formItem.standard = filterData[0].specification
        this.formItem.amount = filterData[0].money
      }
    }
  }
}
</script>
